'use strict'

const _ = require('lodash');
const { TaiSpider } = require('tai-spider');

const siteMap = [{
    idx: 0,
    fn: 'country'
}, {
    idx: 1,
    fn: 'sites',
    parse: (item) => {
        let list = []
        for (let v of item.split('\n')) {
            list.push(v.trim());
        }
        return list;
    },
}]

class GunterSiteSpider extends TaiSpider {

    constructor(options = {}) {
        super(options);
        this.name = 'gunter-site';
        this.debug = true;
        this.start_urls = ['https://space.skyrocket.de/directories/launchsites.htm'];
    }

    *parse(response) {

        let siteEle = response.css('table.index').get(0);

        let sitelist = siteEle.parseTable({
            fieldNameMap: siteMap
        });

        for (let item of sitelist) {
            for (let site of item.sites) {
                let arr = site.split('=');
                yield {
                    index_name: 'tai-site',
                    'id': item.country + ':' + arr[0].trim(),
                    'country': item.country,
                    'abbr': arr[0].trim(),
                    'description': arr[1].trim(),
                };
            }
        }
    }
}

module.exports = GunterSiteSpider;